/*-------------------------------------------------------------------------------
* file:     app_key.h
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2022-08-15
* Version:  V1.00
* Author:   JMH
*-------------------------------------------------------------------------------*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __APP_KEY_NEW_H__
#define __APP_KEY_NEW_H__

/* Includes ------------------------------------------------------------------*/
#include "common.h" 
#include <csi_drv.h>
#include "sys_console.h"
#include "pin.h"
#include "app_cfg.h"
#include "app_init.h"
#include "app_factory.h"
#include "app_lowpower.h"
#include "app_flash_log.h"
#include "app_touchpad.h"




//----------------------------------------
//EVT
#define COLUME0                     PA012   //2
#define COLUME1                     PA013   //4
#define COLUME2                     PA014    //3
#define COLUME3                     PA015    //3
#define COLUME4                     PA10    //3 //
#define COLUME5                     PA12    //3
#define COLUME6                     PA13   //
#define COLUME7                     PA14  //4

#define ROW0                       PA15
#define ROW1                       PC00//PA16
#define ROW2                       PB05
#define ROW3                       PB011//PB06
#define ROW4                       PA16//PB07
#define ROW5                       PA19//PB09
#define ROW6                       PA110//PB011
#define ROW7                       PB07//PA19
#define ROW8                       PB06//PA110
#define ROW9                       PB09//PC00
#define ROW10                      PA00
//#define ROW11                      PA11
//#define ROW12                      PA16
//#define ROW13                      PB05



#define T_FN    0x73

#define KB_A	0x04//4
#define KB_B	0x05//5
#define KB_C	0x06//6
#define KB_D	0x07//7
#define KB_E	0x08//8
#define KB_F	0x09//9
#define KB_G	0x0a//10
#define KB_H	0x0b//11
#define KB_I	0x0c//12
#define KB_J	0x0d//13
#define KB_K	0x0e//14
#define KB_L	0x0f//15
#define KB_M	0x10//16
#define KB_N	0x11//17
#define KB_O	0x12//18
#define KB_P	0x13//19
#define KB_Q	0x14//20
#define KB_R	0x15//21
#define KB_S	0x16//22
#define KB_T	0x17//23
#define KB_U	0x18//24
#define KB_V	0x19//25
#define KB_W	0x1a//26
#define KB_X	0x1b//27
#define KB_Y	0x1c//28
#define KB_Z	0x1d//29
#define KB_1	0x1e//30
#define KB_2	0x1f//31
#define KB_3	0x20//32
#define KB_4	0x21//33
#define KB_5	0x22//34
#define KB_6	0x23//35
#define KB_7	0x24//36
#define KB_8	0x25//37
#define KB_9	0x26//38
#define KB_0	0x27//39
#define KB_Enter		0x28//40
#define KB_Back			0x2a//42
#define KB_Tab			0x2b//43
#define KB_Space		0x2c//44		空格		
#define KB_jianhao		0x2d//45//-_jianhao		
#define KB_denghao		0x2e//46//+= denghao
#define KB_Lguohao		0x2f//47//{[
#define KB_Rguohao		0x30//48//}]
#define KB_xiegang		0x31//49//|\ xiegang
#define KB_K42			0x32//50
#define KB_fenhao		0x33//51//:; fen hao
#define KB_yinhao		0x34//52//" ' yin hao
#define KB_dunhao		0x35//53//~`dun hao 
#define KB_douhao		0x36//54 //<,
#define KB_juhao		0x37//55	//>.	
#define KB_wenhao		0x38//56//?/
#define KB_Caps			0x39//57




#define KB_Esc			0x60
#define	KB_F1      		0x46//58
#define	KB_F2      		0x62//59
#define	KB_F3  			0x63//60


#define	KB_F4   		0x7f//61//静音
#define	KB_F5   		0x81//62
#define	KB_F6			0x80//63
#define	KB_F7 			0xf7//64


#define	KB_F8  			0x68//65
#define	KB_F9   		0x69//66
#define	KB_F10  		0x6a//67
#define	KB_F11  		0x6b//68
#define	KB_F12  		0x6c//69
#define KB_F13  		0x6d
#define KB_Del			0x4c
#define KB_Search		0x6f
#define KB_Voice		0x71

#define KB_Up      		0x52
#define KB_Down 		0x51
#define KB_Left 		0x50
#define KB_Right 		0x4f

#define KB_K45			0x64//57


#define KB_LCtrl  		0xe0
#define KB_LShift 		0xe1
#define KB_LAlt   		0xe2
#define KB_Win    		0xe3
#define KB_RShift 		0xe5
#define KB_RAlt	  		0xe6


// #endif

#define KEY_COLUME_NUMBER           8
#define KEY_ROW_NUMBER              11

#define  KEY_BOTTON_MAX_NUM         12       //最大按键数量
#define  TOTAL_BUTTON_NUMBER        (KEY_ROW_NUMBER*KEY_COLUME_NUMBER)

#define KEY_SCAN_TIMER_N            50//*100US

#define Caps_LED_PIN                PA02
#define Caps_LED_PIN_OUTPUT         PA02_OUTPUT
#define Caps_LED_PIN_GPD            PA02_GPD

//DVT
#define MIC_LED_PIN             PA01
#define MIC_LED_PIN_OUTPUT      PA01_OUTPUT
#define MIC_LED_PIN_GPD         PA01_GPD


typedef enum
{
    LED_CAPS        = 0,
    LED_MIC
}LED_STATUS;

typedef enum
{
    BTN_NONE = 0,
    BTN_DOWN,
    BTN_UP,
    BTN_LONG,
} BTN_STATE;

typedef struct __Button_Typedef
{
    uint16_t press_tick;        /* 按下计数 */
    uint16_t release_tick;      /* 抬起计数 */
    BTN_STATE  state;           /* 按键当前状态（按下还是弹起） */
    uint8_t  value;             /* 按键键值 */
    uint8_t  pos_row;           /* 按键行位置 */
    uint8_t  pos_col;           /* 按键列位置 */
    uint8_t  key_location;      /* 按键绝对位置*/
}Button_Typedef;

typedef struct __Key_Queue_Typedef
{
    Button_Typedef * key_btn; //按键状态结构体
    uint8_t  head;
    uint8_t  tail;
    uint8_t  queue_existing_len; //队列长度
}Button_Queue_typedef;

typedef enum
{
    KEY_TYPE_FREE = 0,
    KEY_TYPE_MEDIA, //多媒体
    KEY_TYPE_GENE, //普通按键
    KEY_TYPE_FN,
}ENUM_KEY_TYPE;

typedef struct key_led_dvt_tab
{
    pin_name_e IO_name;
    pin_func_e IO_mode_output;
    pin_func_e IO_mode_gpd;
}KEY_LED_DVT, KEY_LED_EVT, * KEY_LED_VER;

extern ENUM_KEY_TYPE key_type_back;
extern ENUM_KEY_TYPE key_madia_type_back;
extern volatile uint8_t global_key_number;


extern volatile uint8_t keyboard_led_status;
extern uint8_t Waiting_sleep;
/*led*/
void app_key_led_io_set_init(void);
void app_key_led_set_mode(LED_STATUS LED, uint8_t on);

/*queue*/
bool app_key_add_key_queue_write(Button_Queue_typedef *q,uint8_t location_botton);
void app_key_create_queue(Button_Queue_typedef *q);
bool app_key_cheak_botton(Button_Queue_typedef *q ,uint8_t location_botton);
uint8_t app_key_cheak_release_status(Button_Queue_typedef *q);

/*app*/
void app_key_init(bool f_power_on);
void app_key_process(void);
void app_key_timer_tick_0_1ms_callback(void);
uint8_t app_key_waiting_sleep_process(void);
/*key scan*/
uint8_t app_key_scan(void);
uint8_t app_read_colume_in(void);
void app_key_get_botton_to_queue(uint8_t key_botton_nnumber);
uint8_t app_read_colume_in(void);
uint8_t app_key_bit_num(uint8_t val);
uint8_t app_key_check_ghost(Button_Queue_typedef *q , uint8_t number_key_button);
void app_key_write_to_one_wire_buf(Button_Queue_typedef *q,uint8_t status);
bool app_key_have_press_status(Button_Queue_typedef *q , uint8_t i);
bool app_media_repeat_send(uint8_t status);
bool app_key_repeat_send(uint8_t status);
uint8_t app_key_tick_change(Button_Queue_typedef *q, uint8_t i, bool f_btn_down);

/*api*/
void app_key_all_row_input_pull_up(void);
void app_key_all_row_out_low(void);
void app_key_all_colume_input_pull_up(void);

/*寄存器接口*/
void app_key_io_input_pull_up(uint8_t pin);
void app_key_row_out_low(uint8_t pin);

/*休眠相关*/
void app_enter_factor_mode(bool on);

/**
 * @brief 调试低功耗设置
 * 
 * @param on 
 */
void app_key_low_power_sleep_set(bool on);
#endif
